const vueTemplateCompiler = require('vue-template-compiler');
const defaultConfig = require('./default-config');
const cssUtil = require('./css-util');
const codeUtil = require('./code-util');

module.exports = class Compiler {
    constructor(content, option) {
        this._parsed = vueTemplateCompiler.parseComponent(content);
        this.option = { ...defaultConfig, ...option };
    }

    transform(filePath) {
        let styles = this._parsed.styles.map(style => {
            return style.scoped ? cssUtil.addScopeIdToStyle(style.content, cssUtil.getId()) : style.content;
        });

        let script = codeUtil.transform(this._parsed.script.content);
        let template = this._parsed.template.content;

        return {
            script,
            template,
            style: cssUtil.minify(styles.join(''))
        };
    }
}
